<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\note;

use app\api\model\dao\DashangGift;
use app\api\model\dao\DashangRecord;
use app\api\model\dao\Note;
use app\api\model\dao\NotePraise;
use app\api\model\dao\NoteReply;
use app\api\model\dao\UserAccount;
use app\api\model\dao\UserCurrency;
use app\api\model\data\note\NoteData;
use app\library\Common;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Log;
use think\Validate;

class GetRewardMineList extends PageBase
{

    private $objNote;
    private $objNoteReply;
    private $objNoteData;
    private $objNotePraise;
    private $objDashangRecond;
    private $objUserAccount;
    private $objUserCurrency;
    private $objGift;

    public function __construct()
    {
        parent::__construct();

        $this->objNote = new Note();
        $this->objNoteReply = new NoteReply();
        $this->objNoteData = new NoteData();
        $this->objNotePraise = new NotePraise();
        $this->objDashangRecond = new DashangRecord();
        $this->objUserAccount = new UserAccount();
        $this->objGift = new DashangGift();
        $this->objUserCurrency = new UserCurrency();
    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function excute($param)
    {
        $this->checkToken($param);

        $this->user_id = !empty($this->user_id) ? $this->user_id : (!empty($param['user_id']) ? $param['user_id'] : 0);

        $this->getRewardList();
        $this->getRewardList(false);

        return $this->getRes();
    }

    public function getRewardList($mine = true)
    {
        if ($mine) {
            $conds = [
                'note_userid' => ['=', $this->user_id],
            ];
        } else {
            $conds = [
                'userid' => ['=', $this->user_id],
            ];
        }

        $dashang_recond = $this->objDashangRecond->getDashangRecord($conds, [], $this->offset, $this->limit, 'create_time desc');
        if (empty($dashang_recond)) {
            $dashang_recond = [];
        }

        $total = $this->objDashangRecond->getDashangCountByConds($conds);
        empty($total) && $total = 0;

        $userid = [];
        $giftid = [];
        foreach ($dashang_recond as $val) {
            $userid[] = $val['userid'];
            $giftid[] = $val['gift_id'];
        }

        $userid2User = [];
        if (!empty($userid)) {
            $user_account_dashang = $this->objUserAccount->getUserAccountByUserids($userid);
            if (!empty($user_account_dashang)) {
                $user_account_dashang = $this->modelToArray($user_account_dashang);
                foreach ($user_account_dashang as $val) {
                    $userid2User[$val['id']] = $val;
                }
            }
        }

        $giftid2Gift = [];
        if (!empty($giftid)) {
            $gift = $this->objGift->getDashangGiftByGiftid($giftid, ['id', 'pic']);
            if (!empty($gift)) {
                foreach ($gift as $val) {
                    $giftid2Gift[$val['id']] = $val;
                }
            }
        }

        $data = [];
        foreach ($dashang_recond as $val) {
            $data[] = [
                'userid' => $val['userid'],
                'name' => $userid2User[$val['userid']]['nick_name'],
                'photo' => $userid2User[$val['userid']]['avatar'],
                'money' => $val['money'],
                'gift_name' => $val['gift_name'],
                'gift_pic' => $giftid2Gift[$val['gift_id']]['pic'],
                'time' => Common::caleDate($val['create_time']),
            ];
        }

        if ($mine) {
            $this->data['dashang_mine']['list'] = $data;
            $this->data['dashang_mine']['total'] = $total;
        } else {
            $this->data['mine_dashang']['list'] = $data;
            $this->data['mine_dashang']['total'] = $total;
        }
    }
}
